/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.odhixon.volarme.persistence;

import org.garret.perst.Storage;
import org.garret.perst.StorageFactory;

/**
 * Persistence.
 * @author Ariel Aguirre <odhixon@gmail.com>
 */
public class Persistence {

    private static Persistence instance;
    private static final String DATABASE = "volarme.db";

    public static Persistence getInstance() {
        if (instance == null) {
            instance = new Persistence();
        }
        return instance;
    }

    private Storage storage;

    public void open() {
        storage = StorageFactory.getInstance().createStorage();
        storage.open(DATABASE, Storage.DEFAULT_PAGE_POOL_SIZE);
        Root root = (Root) storage.getRoot();
        if (root == null) {
            root = new Root(storage);
            storage.setRoot(root);
            storage.commit();
        }
    }

    public void close() {
        commit();
        storage.close();
    }

    public void commit() {
        storage.commit();
    }

    public void rollBack() {
        storage.rollback();
    }

    public Root getRoot() {
        return (Root) storage.getRoot();
    }
}
